package com.qding.park.manager.web.conf;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Scope;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.session.data.redis.config.annotation.web.http.RedisHttpSessionConfiguration;
import org.springframework.session.web.http.HeaderHttpSessionStrategy;
import org.springframework.session.web.http.HttpSessionStrategy;
import org.springframework.stereotype.Service;

import com.qding.park.manager.web.container.MyHeaderHttpSessionStrategy;

import redis.clients.jedis.JedisPoolConfig;

@Service
@Scope("singleton")
public class SSOSessionBean {

    private static final Logger logger = LoggerFactory.getLogger(SSOSessionBean.class);

    @Autowired
    private SSOConfig ssoConfig;

    @Bean(name = "redisHttpSessionConfiguration")
    public RedisHttpSessionConfiguration getRedisHttpSessionConfiguration() {
        logger.info("ssoConfig=" + ssoConfig);
        logger.info("ssoConfig config ==> sessionTimeout:{}, sessionRedisHost:{},sessionRedisPort:{} ",
                ssoConfig.getSessionTimeout(),ssoConfig.getSessionRedisHost(),ssoConfig.getSessionRedisPort());
        // 加载配置文件属性
        RedisHttpSessionConfiguration  redisHttpSessionConfiguration=new RedisHttpSessionConfiguration();
        redisHttpSessionConfiguration.setMaxInactiveIntervalInSeconds(ssoConfig.getSessionTimeout());
        return redisHttpSessionConfiguration;
    }

    
    @Bean(name = "headerHttpSessionStrategy")
    public HttpSessionStrategy getHeaderHttpSessionStrategy() {
        logger.info("ssoConfig=" + ssoConfig);
        logger.info("ssoConfig config ==> sessionTimeout:{}, sessionRedisHost:{},sessionRedisPort:{} ",
                ssoConfig.getSessionTimeout(),ssoConfig.getSessionRedisHost(),ssoConfig.getSessionRedisPort());
        // 加载配置文件属性
//        HeaderHttpSessionStrategy  httpSessionStrategy=new HeaderHttpSessionStrategy();
//        httpSessionStrategy.setHeaderName("token");
//        return httpSessionStrategy;
        
        MyHeaderHttpSessionStrategy httpSessionStrategy=new MyHeaderHttpSessionStrategy();
        httpSessionStrategy.setTokenName("token");
        return httpSessionStrategy;
    }
    
    @Bean(name = "jedisConnectionFactory")
    public JedisConnectionFactory getJedisConnectionFactory() throws RuntimeException {
        logger.info("--> JedisConnectionFactory");
        JedisConnectionFactory  factory=new JedisConnectionFactory();
        factory.setPort(ssoConfig.getSessionRedisPort());
        factory.setHostName(ssoConfig.getSessionRedisHost());
        factory.setPoolConfig(getSesssionJedisPoolConfig());
        return factory;
    }
    
    @Bean(name = "sesssionJedisPoolConfig")
    public JedisPoolConfig getSesssionJedisPoolConfig() {
        // 加载配置文件属性
        JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
        jedisPoolConfig.setTestWhileIdle(true);
        jedisPoolConfig.setMinEvictableIdleTimeMillis(60000);
        jedisPoolConfig.setTimeBetweenEvictionRunsMillis(30000);
        jedisPoolConfig.setNumTestsPerEvictionRun(-1);
        jedisPoolConfig.setMaxIdle(5);
        jedisPoolConfig.setMaxTotal(5);
        jedisPoolConfig.setMinIdle(5);
        return jedisPoolConfig;
    }

//    /**
//     * postConstruct(这里用一句话描述这个方法的作用) TODO(这里描述这个方法适用条件 – 可选) TODO(这里描述这个方法的执行流程 – 可选) TODO(这里描述这个方法的使用方法 – 可选)
//     * TODO(这里描述这个方法的注意事项 – 可选) @Title: postConstruct @Description: 通过@PostConstruct 和 @PreDestroy 方法
//     * 实现初始化和销毁bean之前进行的操作 @param 设定文件 @return void 返回类型 @throws
//     */
//    @PostConstruct
//    public void postConstruct() {
//        logger.info("mongoDatasourceConfig={}", mongoDatasourceConfig);
//    }

}
